import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import InvoiceOCRView from '../views/InvoiceOCRView';
import InvoiceRecordView from '../views/InvoiceRecordView';
import InvoiceDetailsView from '../views/InvoiceDetailsView';
import InvoiceSearchView from '@/views/InvoiceSearchView.vue';
import UserProfileView from '../views/UserProfileView';
import LoginView from '../views/LoginView';
import RegisterView from '../views/RegisterView';
import NotFoundView from '../views/NotFoundView';
import store from '@/store';
import TeacherInvoiceView from '@/views/TeacherInvoiceView.vue';
import StudentInvoiceView from '@/views/StudentInvoiceView.vue';

const routes = [
    {
        path: '/',
        name: 'home',
        component: HomeView,
        meta: {
        requestAuth: true,
        }
    },
    {
        path: '/InvoiceOCR/',
        name: 'InvoiceOCR',
        component: InvoiceOCRView,
        meta: {
        requestAuth: true,
        }
    },
    {
        path: '/InvoiceRecord/',
        name: 'InvoiceRecord',
        component: InvoiceRecordView,
        children: [
            {
                path: 'TeacherInvoice/',
                name: 'TeacherInvoice',
                component: TeacherInvoiceView,
                meta: {
                requestAuth: true,
                }
            },
            {
                path: 'StudentInvoice/',
                name: 'StudentInvoice',
                component: StudentInvoiceView,
                meta: {
                requestAuth: true,
                }
            },
        ],
        meta: {
        requestAuth: true,
        }
    },
    {
        path: '/InvoiceDetails/:id',
        name: 'InvoiceDetails',
        component: InvoiceDetailsView,
        meta: {
        requestAuth: true,
        }
    },
    {
        path: '/InvoiceSearch/',
        name: 'InvoiceSearch',
        component: InvoiceSearchView,
        meta: {
        requestAuth: true,
        }
    },
    {
        path: '/userprofile/',
        name: 'userprofile',
        component: UserProfileView,
        meta: {
        requestAuth: true,
        }
    },
    {
        path: '/login/',
        name: 'login',
        component: LoginView,
        meta: {
        requestAuth: false,
        }
    },
    {
        path: '/register/',
        name: 'register',
        component: RegisterView,
        meta: {
        requestAuth: false,
        }
    },
    {
        path: '/404/',
        name: '404',
        component: NotFoundView,
        meta: {
            requestAuth: false,
        }
    },

    // 重定向
    {
        path: '/:catchAll(.*)',
        redirect: "/404/"
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 每次使用router进入某一个页面之前，会先调用这个函数
// to表示跳转到哪个页面，from表示从哪个页面跳转过去的，next表示将页面要不要执行下一步操作
router.beforeEach((to, from, next) => {
    if (to.meta.requestAuth && !store.state.user.is_login) {
        next({name: "login"});
    } else {
        next();
    }
})

export default router
